package com.lzh.team.service;

import com.lzh.team.domain.Architect;
import com.lzh.team.domain.Designer;
import com.lzh.team.domain.Employee;
import com.lzh.team.domain.Equipment;
import com.lzh.team.domain.NoteBook;
import com.lzh.team.domain.PC;
import com.lzh.team.domain.Printer;
import com.lzh.team.domain.Programmer;

import static com.lzh.team.service.Data.*;
/**
 * 
 * @author Dell
 *
 */
public class NameListService {
	
	private Employee[] employees;
	
	/**
	 * 给employee及数组元素进行初始化
	 */
	public NameListService() {
		employees = new Employee[EMPLOYEES.length];
		for(int i = 0;i < employees.length;i++) {
			//获取员工的类型
			int type = Integer.parseInt(EMPLOYEES[i][0]);
			//获取Employee的4个基本信息
			int id = Integer.parseInt(EMPLOYEES[i][1]);
			String name = EMPLOYEES[i][2];
			int age = Integer.parseInt(EMPLOYEES[i][3]);
			double salary = Double.parseDouble(EMPLOYEES[i][4]);
			
			Equipment equipment;
			double bonus;
			int stock;
			
			switch(type) {
			case EMPLOYEE:
				employees[i] = new Employee(id, name, age, salary);
				break;
			case PROGRAMMER:
				equipment = createEquipment(i);
				employees[i] = new Programmer(id, name, age, salary, equipment);
				break;
			case DESIGNER:
				equipment = createEquipment(i);
				bonus = Double.parseDouble(EMPLOYEES[i][5]);
				employees[i] = new Designer(id, name, age, salary, equipment, bonus);
				break;
			case ARCHITECT:
				equipment = createEquipment(i);
				bonus = Double.parseDouble(EMPLOYEES[i][5]);
				stock = Integer.parseInt(EMPLOYEES[i][6]);
				employees[i] = new Architect(id, name, age, salary, equipment, bonus, stock);
				break;
			}
			
		}
	}
	/**
	 * 获取指定index上的员工的设备
	 * @param i
	 * @return
	 * @author liuzhihao
	 * @date 2021年2月9日下午7:27:58
	 */
	private Equipment createEquipment(int index) {
		int type = Integer.parseInt(EQIPMENTS[index][0]);
		
		String modelOrName = EMPLOYEES[index][1];
		switch(type) {
		case PC:
			String display = EQIPMENTS[index][2];
			return new PC(modelOrName,EQIPMENTS[index][2]);
		case NOTEBOOK:
			double price = Double.parseDouble(EQIPMENTS[index][2]);			
			return new NoteBook(modelOrName,price);
		case PRINTER:
			
			return new Printer(modelOrName,EQIPMENTS[index][2]);
		}
		return null;
	}
/**
 * 
 * 获取当前所有的员工
 * @return
 * @author liuzhihao
 * @date 2021年2月9日下午8:00:58
 */
	public Employee[] getAllEmployees() {
		
		return employees;
	}
	/**
	 * 
	 * 获取指定ID的员工对象
	 * @param id
	 * @return
	 * @author liuzhihao
	 * @throws TeamException 
	 * @date 2021年2月9日下午8:01:39
	 */
	public Employee getEmployee(int id) throws TeamException {
		for(int i= 0;i < employees.length;i++) {
			if(employees[i].getId() == id) {
				return employees[i];
			}
		}
		throw new TeamException("找不到指定的员工");
	}
}
